import QtQuick
import QtQuick.Controls
import Tool 1.0
Item {
    id:kChildRectParent
    property bool editMode: false
    property double scaleSize: 1
    property int baseHeight: 50
    property int baseHeight2: baseHeight*scaleSize
    property ListModel kchildRect_listmodel
    property var kchildRect_listmodel_parent
    property int borderWidth: 2
    property int borderColor: Material.Blue
    property int kz: 2000
    property int baseDrawerHeight:1000
    property int baseDrawerWidth:1100
    property int compInt: -1
    property var compVector: []
    // property int j
    // property ListModel kchildRect_listmodel_content: kchildRect_listmodel["content"]
    // ListModel{
    //     id:kchildRect_listmodel

    property int nowJ:-1


    // }
    function extractFileName(filePath) {
        // 使用正则表达式匹配文件名
        var fileName = filePath.replace(/^.*[\\\/]/, '');
        return fileName;
    }

    function transferChildRect(i1,j1,i2){
        var kchildRect_listmodelNum1= kchildRect_listmodel.get(i1)
        var kchildRect_listmodelNum2=kchildRect_listmodel.get(i2);
        if(kchildRect_listmodelNum1&&kchildRect_listmodelNum2){
            var kchildRect_listmodelNum1ChildRect=kchildRect_listmodelNum1.childRect
            var kchildRect_listmodelNum1TransferChildRect=kchildRect_listmodelNum1ChildRect.get(j1)
            console.log(kchildRect_listmodelNum1ChildRect.get(j1))
            var kchildRect_listmodelNum2ChildRect=kchildRect_listmodelNum2.childRect
            if(kchildRect_listmodelNum1ChildRect&&kchildRect_listmodelNum2ChildRect){
                var test= kchildRect_listmodelNum2ChildRect.append(kchildRect_listmodelNum1TransferChildRect)
                // kchildRect_listmodelNum2ChildRect.set(kchildRect_listmodelNum2ChildRect.count,kchildRect_listmodelNum1TransferChildRect)
                // console.log(kchildRect_listmodelNum2ChildRect.count)
                // console.log(kchildRect_listmodelNum1TransferChildRect.i,kchildRect_listmodelNum1TransferChildRect.j)

                // console.log(kchildRect_listmodelNum1TransferChildRect.i,kchildRect_listmodelNum1TransferChildRect.j)
                kchildRect_listmodelNum1ChildRect.remove(j1)
                for(var j=j1;j<kchildRect_listmodelNum1ChildRect.count;j++){
                    kchildRect_listmodelNum1ChildRect.get(j).j--
                }
                // i1=i2
                // j1=kchildRect_listmodelNum2ChildRect.count-1
                // console.log("suc")
                console.log(kchildRect_listmodelNum2ChildRect.count-1)
                return kchildRect_listmodelNum2ChildRect.count-1;
            }
        }
    }

    Component{
        id:childRect_text
        Rectangle{
            clip: true
            property string textAreaText
            property bool resizing: false
            property var startPos: Qt.point(0,0)
            property var minWandH: Qt.point(10,1)
            property int ky
            property int j
            property int i
            property double xbegin:0.1
            property double xend:1
            property int baseHeightInt
            property int kheight:baseHeight*baseHeightInt
            // property string fileNotes
            // property string fileLocation

            property double realY: ky*baseHeight
            property int kwidth
            property double xStart: xbegin

            property int borderWidth: 3
            property color borderColor:color_MaterialBlue
            property color borderColorNotActivited: "grey"
            property bool activited: nowJ===j?true:false

            property int kroundScale: 10
            property bool firstKwidthChanged: true
            property bool firstXStartChanged: true
            property bool firstBaseHeightIntChanged: true
            property bool firstRealYChanged: true


            y:realY
            x:xStart
            width: kwidth
            height:kheight
            border.color: {
                if(!editMode)
                    return borderColor
                return activited?borderColor:borderColorNotActivited

            }
            border.width: borderWidth
            radius: kroundScale

            onKwidthChanged: {
                if(firstKwidthChanged) {firstKwidthChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.width=kwidth
            }

            onXStartChanged: {
                if(firstXStartChanged) {firstXStartChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.xbegin=xStart
            }

            onBaseHeightIntChanged: {
                if(firstBaseHeightIntChanged) {firstBaseHeightIntChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(baseHeightInt)
                theTargetChildRect.height=baseHeightInt
                // console.log("aaaaaaaaaaa",theTargetChildRect.height)
            }



            onRealYChanged: {
                if(firstRealYChanged) {firstRealYChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(theTargetChildRect.realY)
                // console.log(theTargetChildRect.realY/baseHeight2)
                theTargetChildRect.sky=fileFirstRect.realY/baseHeight
            }
            id:fileFirstRect

            Menu{
                id:childRectTextImageMenu
                MenuItem{
                    text:"删除"
                    onTriggered: {
                        var toDeleteComp= compVector.pop(j)
                        toDeleteComp.destroy()
                        kchildRect_listmodel.get(0).childRect.remove(j)
                        for(var i=j+1;j<compVector.length;i++){
                            compVector[i].j--
                        }
                    }

                }
                MenuItem{
                    text:"复制"
                    onTriggered: {}
                }

            }

            KAnchorScaleComp{
                j:fileFirstRect.j
                baseHeight2:kChildRectParent.baseHeight2
                target: fileFirstRect
                baseHeight: kChildRectParent.baseHeight
                scaleSize: kChildRectParent.scaleSize
            }

            Rectangle {
                width: parent.width-borderWidth*2-2
                height: parent.height-borderWidth*2-2
                anchors.centerIn: parent
                TextEdit{
                    id:textEditText
                    onEditingFinished: {
                        textAreaText=textEditText.text
                        var targetChildRect= getTheChildRect(0,j)
                        targetChildRect.text=textAreaText
                    }

                    anchors.fill: parent
                    text:textAreaText
                    // enabled: editMode
                    wrapMode: TextEdit.Wrap
                    font.pointSize: 12
                    // property bool textEditEditMode: false

                    MouseArea{

                        acceptedButtons: Qt.LeftButton | Qt.RightButton
                        drag{
                            target: editMode?fileFirstRect:null
                            minimumX: 0
                            minimumY: 0
                        }
                        // enabled: editMode
                        preventStealing: true
                        width: parent.width
                        height: {
                            if(!editMode)
                                return parent.height
                            if(!fileFirstRect.activited)
                                return parent.height
                            return 0

                        }
                        onClicked: (mouse)=>{
                                       if(mouse.button===Qt.RightButton){
                                           childRectTextImageMenu.popup()
                                       }
                                       nowJ=j

                                   }
                        property int gridSize: baseHeight
                        property int yStart
                        onPressed:(mouse)=> {
                                      if(mouse.button!==Qt.LeftButton){
                                          return
                                      }
                                      yStart=mouseY

                                      // console.log(123333)

                                      // fileFirstRect.realY=50
                                  }
                        onReleased: {
                            // if(mouse.button!==Qt.LeftButton){
                            //     return
                            // }


                            var maxY=(kchildRect_listmodel.count-1)
                            var minY=0

                            var newY=Math.round(fileFirstRect.y/gridSize)*gridSize

                            var newSky=Math.round(fileFirstRect.y/gridSize)
                            // // console.log("sky",newSky)
                            var theChildRect= getTheChildRect(0,j)
                            // console.log(newSky)
                            // console.log(theChildRect.sky)
                            theChildRect.sky=Math.max(minY,Math.min(newSky,maxY))/*newSky*/
                            fileFirstRect.realY=newY

                            //以上为y处理部分
                            //以下为x处理部分


                            var maxX=kdrawer.width
                            var minX=0
                            theChildRect.xbegin=(Math.max(minX,Math.min(fileFirstRect.x,maxX)))



                        }


                        onDoubleClicked: {
                            nowJ=j
                            // fileFirstRect.activited=true
                        }
                        // editMode?parent.height:0

                    }
                    // on
                }



            }



        }
    }

    function getTheChildRect(i,j){
        return kchildRect_listmodel.get(i).childRect.get(j)
    }




    Component{
        id:compOfKFileOpenPage
        KFileOpenPage{

        }

    }

    Connections {
        target: Tool
        function onSendFileOpenInfo(info,infoPath) {
            // 创建 KFileOpenPage 的实例
            var fileOpenPageInstance = compOfKFileOpenPage.createObject(parent, {
                                                                            fileOpenPageTextAreaText: info, // 传递 info
                                                                            filePath:infoPath
                                                                        });

            if (fileOpenPageInstance) {
                // 使用创建的实例添加页面
                main_ktabbarview.appendPage(fileOpenPageInstance);
            } else {
                console.log("Failed to create KFileOpenPage instance");
            }
        }
    }

    Component{
        id:childRect_table
        Rectangle{
            id:fileFirstRect


            // property ListModel tableListModela:{
            // }
            property alias tableListModelAlias: internalTableModel // 暴露给外部的别名

            ListModel {
                id: internalTableModel // 内部真正存储数据的 Model
                onCountChanged: {
                    fileFirstRect.kCol=internalTableModel.get(0)["rRow"].count

                }

            }

            // property ListModel tableListModel:{}
            property int kCol
            property int kRow:internalTableModel.count

            property bool resizing: false
            property var startPos: Qt.point(0,0)
            property var minWandH: Qt.point(400,1)
            property int ky
            property int j
            property int i
            property double xbegin:0.1
            property double xend:1
            property int baseHeightInt
            property int kheight:baseHeight*baseHeightInt


            property double realY: ky*baseHeight
            property int kwidth
            property double xStart: xbegin

            property int borderWidth: 3
            property color borderColor:color_MaterialBlue
            property int kroundScale: 10
            property bool firstKwidthChanged: true
            property bool firstXStartChanged: true
            property bool firstBaseHeightIntChanged: true
            property bool firstRealYChanged: true
            property bool activited: nowJ===j
            property color notActivitedColor: "grey"

            Menu{
                id:childRectTableImageMenu
                MenuItem{
                    text:"删除"
                    onTriggered: {
                        var toDeleteComp= compVector.pop(j)
                        toDeleteComp.destroy()
                        kchildRect_listmodel.get(0).childRect.remove(j)
                        for(var i=j+1;j<compVector.length;i++){
                            compVector[i].j--
                        }
                    }

                }
                MenuItem{
                    text:"复制"
                    onTriggered: {



                    }
                }

            }

            onKwidthChanged: {
                if(firstKwidthChanged) {firstKwidthChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.width=kwidth
            }

            onXStartChanged: {
                if(firstXStartChanged) {firstXStartChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.xbegin=xStart
            }

            onBaseHeightIntChanged: {
                if(firstBaseHeightIntChanged) {firstBaseHeightIntChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(baseHeightInt)
                theTargetChildRect.height=baseHeightInt
                // console.log("aaaaaaaaaaa",theTargetChildRect.height)
            }



            onRealYChanged: {
                if(firstRealYChanged) {firstRealYChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(theTargetChildRect.realY)
                // console.log(theTargetChildRect.realY/baseHeight2)
                theTargetChildRect.sky=fileFirstRect.realY/baseHeight
            }

            // z:1000-i
            y:realY
            x:xStart
            width: kwidth
            height:kheight
            // border.color: color_MaterialBlue
            border.color: activited?color_MaterialBlue:notActivitedColor
            border.width: borderWidth
            radius: kroundScale

            KAnchorScaleComp{
                j:fileFirstRect.j
                baseHeight2:kChildRectParent.baseHeight2
                target: fileFirstRect
                baseHeight: kChildRectParent.baseHeight
                scaleSize: kChildRectParent.scaleSize
            }



            Component {
                id: rectOfTable
                Rectangle {
                    color: "white"
                    border.color: "grey"
                    border.width: 1
                    clip: true
                    ScrollView{
                        anchors.fill: parent
                        Text {
                            anchors.centerIn: parent
                            text:textContent
                            // text: `Row: ${rowIndex}, Col: ${colIndex}`
                        }

                    }

                }
            }





            // 外层 ListView 用于生成行
            ListView {

                MouseArea{
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
                    width: parent.width
                    preventStealing: true
                    height: {
                        if(activited)
                            return 0;
                        return parent.height
                    }

                    onDoubleClicked: {
                        // console.log(123)
                        if(!editMode){
                            return
                        }
                        // activited=false
                        nowJ=j
                        // console.log(nowJ)
                    }

                    onClicked:(mouse)=>{
                                  // console.log(mouse.button)
                                  if(mouse.button===Qt.RightButton){
                                      // console.log(12312312313)
                                      childRectTableImageMenu.popup()
                                  }
                                  // nowJ=j

                              }
                    drag{
                        target: fileFirstRect
                        minimumX: 0
                        minimumY: 0

                    }
                    property int gridSize: baseHeight
                    property int yStart
                    onPressed:(mouse)=> {
                                  if(mouse.button!==Qt.LeftButton){
                                      return
                                  }
                                  yStart=mouseY

                                  // console.log(123333)

                                  // fileFirstRect.realY=50
                              }
                    onReleased: {
                        // if(mouse.button!==Qt.LeftButton){
                        //     return
                        // }


                        var maxY=(kchildRect_listmodel.count-1)
                        var minY=0

                        var newY=Math.round(fileFirstRect.y/gridSize)*gridSize

                        var newSky=Math.round(fileFirstRect.y/gridSize)
                        // // console.log("sky",newSky)
                        var theChildRect= getTheChildRect(0,j)
                        // console.log(newSky)
                        // console.log(theChildRect.sky)
                        theChildRect.sky=Math.max(minY,Math.min(newSky,maxY))/*newSky*/
                        fileFirstRect.realY=newY

                        //以上为y处理部分
                        //以下为x处理部分


                        var maxX=kdrawer.width
                        var minX=0
                        theChildRect.xbegin=(Math.max(minX,Math.min(fileFirstRect.x,maxX)))



                    }



                }
                id: listviewOfTable
                // anchors.fill: parent
                anchors.centerIn: parent
                width: parent.width-10
                height: parent.height-15
                model:internalTableModel // 行数
                spacing: 1 // 行间距
                delegate: Item {
                    width: listviewOfTable.width
                    height: listviewOfTable.height / kRow
                    // 内层 Repeater 用于生成列
                    Component.onCompleted: {
                        console.log(kRow,kCol)


                    }
                    Row {
                        anchors.fill: parent
                        spacing: 1 // 列间距
                        Repeater {
                            model: rRow// 列数
                            delegate: Loader {
                                width: parent.width / kCol
                                height: parent.height
                                sourceComponent: rectOfTable
                                property string textContent:text
                                // property int rowIndex: index // 当前行索引
                                // property int colIndex: modelData // 当前列索引
                            }
                        }
                    }
                }
            }











        }
    }



    Component{
        id:childRect_file
        Rectangle{

            id:fileFirstRect


            property bool resizing: false
            property var startPos: Qt.point(0,0)
            property var minWandH: Qt.point(400,1)
            property int ky
            property int j
            property int i
            property double xbegin:0.1
            property double xend:1
            property int baseHeightInt
            property int kheight:baseHeight*baseHeightInt
            property string fileNotes
            property string fileLocation

            property double realY: ky*baseHeight
            property int kwidth
            property double xStart: xbegin

            property int borderWidth: 3
            property color borderColor:color_MaterialBlue
            property int kroundScale: 10
            property bool firstKwidthChanged: true
            property bool firstXStartChanged: true
            property bool firstBaseHeightIntChanged: true
            property bool firstRealYChanged: true

            onKwidthChanged: {
                if(firstKwidthChanged) {firstKwidthChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.width=kwidth
            }

            onXStartChanged: {
                if(firstXStartChanged) {firstXStartChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                theTargetChildRect.xbegin=xStart
            }

            onBaseHeightIntChanged: {
                if(firstBaseHeightIntChanged) {firstBaseHeightIntChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(baseHeightInt)
                theTargetChildRect.height=baseHeightInt
                // console.log("aaaaaaaaaaa",theTargetChildRect.height)
            }



            onRealYChanged: {
                if(firstRealYChanged) {firstRealYChanged=false;return}
                var theTargetChildRect=getTheChildRect(0,j)
                // console.log(theTargetChildRect.realY)
                // console.log(theTargetChildRect.realY/baseHeight2)
                theTargetChildRect.sky=fileFirstRect.realY/baseHeight
            }




            // z:1000-i
            y:realY
            x:xStart
            width: kwidth
            height:kheight
            border.color: borderColor
            border.width: borderWidth
            radius: kroundScale
            // Component.onCompleted: {
            //     console.log(x,realY,kwidth,kheight)
            // }
            KAnchorScaleComp{
                j:fileFirstRect.j
                baseHeight2:kChildRectParent.baseHeight2
                target: fileFirstRect
                baseHeight: kChildRectParent.baseHeight
                scaleSize: kChildRectParent.scaleSize
            }






            // scale: scaleSize
            // scale:kroundScale
            // color:"grey"
            // anchors.fill: parent
            Rectangle {


                width: parent.width-borderWidth*2-2
                height: parent.height-borderWidth*2-2
                anchors.centerIn: parent
                // MouseArea{
                //     // z:editMode?1:2
                //     property double yStart
                //     property double gridSize: scaleSize*baseHeight
                //     anchors.fill: parent
                //     enabled: editMode
                //     onClicked: {
                //         // console.log(123333)
                //         yStart=mouseY
                //     }
                //     drag{
                //         target: fileFirstRect
                //         axis:Drag.XAndYAxis
                //         minimumX: 0

                //     }
                //     onReleased: {
                //         var  maxY=scaleSize*baseHeight*(kchildRect_listmodel.count-i)
                //         var  minY=-i*scaleSize*baseHeight
                //         console.log(maxY)

                //         var newY=Math.round(fileFirstRect.y/gridSize)*gridSize
                //         // console.log(newY,maxY)
                //         if(newY>=maxY){
                //             fileFirstRect.y=Math.min(yStart,newY)
                //             return
                //         }

                //         fileFirstRect.y=Math.max(newY,minY)


                //     }
                // }




                Row{
                    spacing:10
                    anchors.fill: parent
                    Image {
                        // z:editMode?1:2
                        id:fileImg
                        height: parent.height
                        width: height
                        source: "qrc:/ICON/icon/file.png"

                        MouseArea{
                            width: parent.width
                            height: editMode?0:parent.height
                            onDoubleClicked: {
                                // console.log(123)
                                Tool.readFileOpenInfo(textfiled_location.text);


                            }


                        }

                        Menu{
                            id:childRectFileImageMenu
                            MenuItem{
                                text:"删除"
                                onTriggered: {
                                    var toDeleteComp= compVector.pop(j)
                                    toDeleteComp.destroy()
                                    kchildRect_listmodel.get(0).childRect.remove(j)
                                    for(var i=j+1;j<compVector.length;i++){
                                        compVector[i].j--
                                    }
                                }

                            }
                            MenuItem{
                                text:"复制"
                                onTriggered: {}
                            }

                        }

                        MouseArea{
                            // z:editMode?1:2
                            // preventStealing: true
                            property double yStart
                            property double gridSize:baseHeight
                            property double xStart
                            width: parent.width
                            height: editMode?parent.height:0
                            // enabled: editMode
                            acceptedButtons: Qt.LeftButton | Qt.RightButton

                            // drag{
                            //     target: fileFirstRect
                            //     axis: Drag.XAndYAxis
                            //     // minimumX: 0
                            //     // maximumX: dragRect.parent.width - dragRect.width
                            //     // minimumY: 0
                            //     // maximumY: dragRect.parent.height - dragRect.height
                            // }
                            // // property real gridSize: 100 // 每次移动的倍数距离（例如 20 像素）
                            // onReleased: {
                            //     // 对齐 X 坐标


                            //     // 对齐 Y 坐标
                            //     var alignedY = Math.round(fileFirstRect.y / gridSize) * gridSize;
                            //     fileFirstRect.y = Math.max(0, Math.min(alignedY, 500));
                            // }
                            onClicked: (mouse)=>{
                                           if(mouse.button===Qt.RightButton){
                                               childRectFileImageMenu.popup()
                                           }
                                           nowJ=j

                                       }

                            onPressed:(mouse)=> {
                                          if(mouse.button!==Qt.LeftButton){
                                              return
                                          }else{
                                              yStart=mouseY
                                          }
                                          // console.log(123333)

                                          // fileFirstRect.realY=50
                                      }
                            drag{
                                // color:"yellow"
                                target: fileFirstRect
                                axis:Drag.XAndYAxis
                                minimumX: 0

                            }

                            // onCanceled:

                            onReleased:(mouse)=> {
                                           if(mouse.button!==Qt.LeftButton){
                                               return
                                           }
                                           // fileFirstRect.realY=50

                                           // var  maxY=scaleSize*baseHeight*(kchildRect_listmodel.count-i)
                                           // var  minY=-i*scaleSize*baseHeight
                                           // // console.log(maxY)

                                           // var newY=Math.round(fileFirstRect.y/gridSize)*gridSize
                                           // // console.log(newY,maxY)
                                           // if(newY>=maxY){
                                           //     fileFirstRect.y=Math.min(yStart,newY)

                                           //     return
                                           // }
                                           // console.log(i,j)
                                           // var finalY=Math.max(newY,minY)
                                           // fileFirstRect.y=finalY
                                           // var newI=(finalY-yStart)/(scaleSize*baseHeight)+i

                                           var maxY=(kchildRect_listmodel.count-1)
                                           var minY=0

                                           var newY=Math.round(fileFirstRect.y/gridSize)*gridSize
                                           // if(newY>=maxY){
                                           //     fileFirstRect.realY=Math.min(yStart,newY)
                                           //     return
                                           // }
                                           // var finalY=Math.max(newY,minY)
                                           // // fileFirstRect.realY=finalY
                                           var newSky=Math.round(fileFirstRect.y/gridSize)
                                           // // console.log("sky",newSky)
                                           var theChildRect= getTheChildRect(0,j)
                                           // console.log(newSky)
                                           // console.log(theChildRect.sky)
                                           theChildRect.sky=Math.max(minY,Math.min(newSky,maxY))/*newSky*/
                                           fileFirstRect.realY=newY
                                           // Math.max(minY,Math.min(maxY*gridSize,newY))/*newSky*gridSize*/
                                           // console.log(maxY*gridSize)
                                           // console.log("newSky*gridSize",Math.max(minY,Math.min(maxY*gridSize,newY)))

                                           //以上为y处理部分
                                           //以下为x处理部分


                                           var maxX=kdrawer.width
                                           var minX=0
                                           theChildRect.xbegin=(Math.max(minX,Math.min(fileFirstRect.x,maxX)))



                                           // j=transferChildRect(i,j,newI)
                                           // console.log()
                                           // i=newI
                                           // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                           // console.log(i,j)


                                       }
                        }
                    }
                    ScrollView{
                        height:parent.height
                        width:parent.width-height
                        Column{
                            anchors.left: parent.left
                            anchors.margins: 5
                            spacing:5
                            width: parent.width
                            height: contentItem.childrenRect.height
                            property int contentRowHeight: 30
                            Row{
                                width: parent.width
                                height:30
                                Text{
                                    anchors.verticalCenter: parent.verticalCenter
                                    // anchors.right: parent.right
                                    height: parent.height
                                    width: parent.width/10*3
                                    text:"文件名称："
                                    font.pixelSize: 18
                                    verticalAlignment: Text.AlignVCenter
                                    horizontalAlignment: Text.AlignHCenter

                                }
                                TextField{
                                    id:textfiled_fileName
                                    // z:editMode?3:1
                                    MouseArea{
                                        id:mouseAreaFileName
                                        property bool isEditable:false
                                        width: parent.width
                                        // height: editMode?0:parent.height
                                        height: parent.height
                                        // onClicked: {
                                        // }
                                        // onDoubleClicked: {
                                        //     if(!editMode){
                                        //         return
                                        //     }
                                        //     height=0
                                        //     // isEditable=!isEditable
                                        // }

                                    }

                                    font.pointSize: 8
                                    // enabled:mouseAreaFileLocation&&editMode
                                    enabled: true
                                    // anchors.right: parent.right
                                    anchors.verticalCenter: parent.verticalCenter
                                    height: parent.height
                                    width: parent.width/5*3-15
                                    text:extractFileName(fileLocation)
                                }

                            }

                            Row{
                                width: parent.width
                                height:30
                                Text{
                                    // anchors.ri*/ght: parent.right
                                    anchors.verticalCenter: parent.verticalCenter
                                    height: parent.height
                                    width: parent.width/10*3
                                    text:"文件地址："
                                    font.pixelSize: 18
                                    verticalAlignment: Text.AlignVCenter
                                    horizontalAlignment: Text.AlignHCenter
                                }
                                TextField{
                                    id:textfiled_location
                                    // z:editMode?3:1
                                    MouseArea{
                                        id:mouseAreaFileLocation
                                        property bool isEditable:false
                                        width: parent.width
                                        height: editMode?0:parent.height

                                        // onClicked: {
                                        // }
                                        // onDoubleClicked: {
                                        //     if(!editMode){
                                        //         return
                                        //     }
                                        //     height=0
                                        //     // isEditable=!isEditable
                                        // }

                                    }

                                    font.pointSize: 8
                                    // enabled:mouseAreaFileLocation&&editMode
                                    enabled: true
                                    // anchors.right: parent.right
                                    anchors.verticalCenter: parent.verticalCenter
                                    height: parent.height
                                    width: parent.width/5*3-15
                                    text:fileLocation

                                    onEditingFinished: {
                                        fileLocation=textfiled_location.text
                                        var targetChildRect= getTheChildRect(0,j)
                                        targetChildRect.location=textfiled_location.text
                                        // console.log(targetChildRect.fileNotes)
                                        textfiled_fileName.text=extractFileName(fileLocation)
                                        // listModelToObject(kCodeNestTreeView_ListModel)//保存


                                    }
                                }
                            }


                            Row{
                                // z:0
                                width: parent.width
                                height:30
                                Text{
                                    // anchors.ri*/ght: parent.right
                                    anchors.verticalCenter: parent.verticalCenter
                                    height: parent.height
                                    width: parent.width/10*3
                                    text:"文件备注："
                                    font.pixelSize: 18
                                    verticalAlignment: Text.AlignVCenter
                                    horizontalAlignment: Text.AlignHCenter
                                }
                                TextField{


                                    MouseArea{
                                        id:mouseAreaFileNotes
                                        property bool isEditable:false
                                        width: parent.width
                                        height: editMode?0:parent.height
                                        // onClicked: {
                                        // }
                                        // onDoubleClicked: {
                                        //     if(!editMode){
                                        //         return
                                        //     }
                                        //     height=0
                                        //     // isEditable=!isEditable
                                        // }

                                    }
                                    id:textfield_fileNotes
                                    font.pointSize: 8
                                    // enabled:mouseAreaFileLocation&&editMode
                                    enabled: true
                                    // anchors.right: parent.right
                                    anchors.verticalCenter: parent.verticalCenter
                                    height: parent.height
                                    width: parent.width/5*3-15
                                    text:fileNotes

                                    onEditingFinished: {
                                        fileNotes=textfield_fileNotes.text
                                        var targetChildRect= getTheChildRect(0,j)
                                        targetChildRect.notes=textfield_fileNotes.text
                                        // console.log(targetChildRect.fileNotes)

                                        // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                        // console.log(123123)



                                    }
                                }
                            }



                        }
                    }
                }
            }


        }
    }

    function positionedComp(notPositionedY){
        var minY=0
        var maxY=(kchildRect_listmodel.count-1)*baseHeight
        var newY=Math.round(notPositionedY/baseHeight2)*baseHeight2
        return Math.max(minY,Math.min(newY,maxY))

    }
    function positionedCompWithJ(j){
        var newY=j*baseHeight
        return newY

    }


    function createAFile(sky,xbegin){
        return {
            "height":2,
            "location":"",
            "notes":"",
            "sky":sky,
            "type":"File",
            "width":400,
            "xbegin":xbegin,
            "xend":0
        }
    }
    function createAText(sky,xbegin){
        return {
            "height":2,
            "sky":sky,
            "type":"File",
            "width":400,
            "xbegin":xbegin,
            "xend":0
        }
    }


    Rectangle{
        // spacing: 5
        anchors.fill: parent
        color:"grey"

        ScrollView{
            // x:kdrawer.x>=0?0:kdrawer.x
            // y:kdrawer.y>=0?0:kdrawer.y
            // x:-100
            // y:-100

            // anchors.top: parent.top
            // anchors.margins: 19
            height: parent.height-45
            width: parent.width
            id:kdrawer_parent_scrollview
            // contentWidth: kdrawer_parent.width  // 内容宽度跟随父容器
            // contentHeight: kdrawer_parent.height // 内容高度跟随父容器

            Rectangle{
                scale: scaleSize
                // x:0
                // y:0


                id:kdrawer_parent
                color: "transparent"
                // width: parent.width
                // height: parent.height
                // x:-100
                // y:-100
                // width: parent.width+100
                // height: parent.height+100
                // anchors.fill: parent
                // width: x+width<parent.width?parent.width:parent.width+(width-(parent.width-x))
                // height:y+height<parent.height?parent.height:parent.height+(height-(parent.height-y))
                // x:contentItem.childrenRect.x>=0?0:




                // width: contentItem.childrenRect.x+contentItem.childrenRect.width<parent.width?
                //            parent.width:
                //            parent.width+contentItem.childrenRect.width-contentItem.childrenRect.x
                // height:contentItem.childrenRect.y+contentItem.childrenRect.height<parent.height?
                //            parent.height:
                //            parent.height+(contentItem.childrenRect.height-( parent.height-contentItem.childrenRect.y))


                Rectangle{




                    id:kdrawer
                    width :baseDrawerWidth
                    height: contentItem.childrenRect.height<baseHeight*15?baseHeight*15:contentItem.childrenRect.height
                    // contentItem.childrenRect.height<scaleSize*baseHeight2*15?scaleSize*baseHeight2*15:contentItem.childrenRect.height
                    // Component.onCompleted: {
                    //     console.log(height)
                    // }

                    Component.onCompleted:{
                        var ppw=kdrawer_parent_scrollview.width;
                        var pph=kdrawer_parent_scrollview.height;
                        // parent.width=x+width<ppw?ppw:ppw+(width-(ppw-x))
                        // parent.height=y+height<pph?pph:pph+(height-(pph-y))
                        kdrawer_parent_scrollview.contentWidth=x+width<ppw?ppw:ppw+(width-(ppw-x))
                        kdrawer_parent_scrollview.contentHeight=y+height<pph?pph:pph+(height-(pph-y))
                    }
                    // onXChanged: {
                    //     kdrawer_parent_scrollview.contentWidth = Math.max(
                    //                 kdrawer_parent_scrollview.width,
                    //                 x + width
                    //                 )
                    // }
                    // onYChanged: {
                    //     kdrawer_parent_scrollview.contentHeight = Math.max(
                    //                 kdrawer_parent_scrollview.height,
                    //                 y + height
                    //                 )
                    // }
                    onXChanged: {
                        var ppw=kdrawer_parent_scrollview.width;
                        kdrawer_parent_scrollview.contentWidth=x+width<ppw?ppw:ppw+(width-(ppw-x))
                        console.log(parent.width)
                    }
                    onYChanged: {
                        var pph=kdrawer_parent_scrollview.height;
                        kdrawer_parent_scrollview.contentHeight=y+height<pph?pph:pph+(height-(pph-y))
                    }



                    MouseArea{
                        width: parent.width
                        height: editMode?0:parent.height
                        // anchors.fill: parent



                        // 关键点 5: 自动扩展滚动区域

                        drag{
                            target: kdrawer
                            minimumX: -(kdrawer.width-50)
                            minimumY: -(kdrawer.height-50)


                        }


                    }


                    // anchors.centerIn: parent
                    // anchors.horizontalCenter: parent.horizontalCenter
                    // color:"blue"
                    ListView{
                        interactive: false
                        // MouseArea{
                        //     width: parent.width
                        //     height: editMode?0:parent.height
                        //     // anchors.fill: parent
                        //     onClicked: {


                        //     }
                        //     drag{
                        //         target: kdrawer
                        //         minimumX: 0
                        //         minimumY: 0


                        //     }


                        // }
                        // anchors.fill: parent
                        // Rectangle{
                        //     anchors.fill: parent
                        //     color:"black"
                        // }
                        width: parent.width
                        height: contentItem.childrenRect.height
                        model:kchildRect_listmodel
                        id:drawer_listview
                        delegate: Rectangle{
                            width: drawer_listview.width
                            height:baseHeight
                            // Rectangle{
                            //     anchors.fill: parent
                            //     color:"yellow"
                            // }
                            // MouseArea{
                            //     z:0
                            //     anchors.fill: parent
                            //     onClicked: {
                            //     }
                            //     drag{
                            //         target: kdrawer
                            //         minimumX: 0
                            //         minimumY: 0


                            //     }


                            // }

                            z:-index
                            // z:2
                            // color: "transparent"


                            property int blackBottomHeight: 2

                            // scale: scaleSize
                            MouseArea{
                                id:item_contentParentMouseArea
                                anchors.fill: parent

                                onClicked: {
                                    // console.log(compInt)
                                    switch(compInt){

                                    case -1:{
                                        nowJ=-1
                                        return
                                    }

                                    case 0:{//file
                                        var actualSkY=index
                                        console.log(actualSkY)
                                        var theFirstContent=kchildRect_listmodel.get(0).childRect
                                        var createdFile=createAFile(actualSkY,mouseX)
                                        theFirstContent.append(createdFile)
                                        var childRectFile= childRect_file.createObject(drawer_listview.contentItem.children[0])
                                        // childRectFile.z=0
                                        childRectFile.i=0
                                        childRectFile.ky=createdFile.sky<kchildRect_listmodel.count?createdFile.sky:kchildRect_listmodel.count-1
                                        childRectFile.j=kchildRect_listmodel.get(0).childRect.count-1
                                        childRectFile.xbegin=createdFile.xbegin
                                        childRectFile.xend=createdFile.xend
                                        childRectFile.baseHeightInt=createdFile.height
                                        // childRectFile.kheight=baseHeight*
                                        childRectFile.fileNotes=createdFile.notes
                                        childRectFile.fileLocation=createdFile.location
                                        childRectFile.kwidth=createdFile.width
                                        compVector.push(childRectFile)


                                        break;
                                    }
                                    case 1:{//text
                                        var actualSkYText=index
                                        // console.log(actualY)
                                        var theFirstContentText=kchildRect_listmodel.get(0).childRect


                                        var createdText=createAText(actualSkYText,mouseX)
                                        theFirstContentText.append(createdText)
                                        var childRectText= childRect_text.createObject(drawer_listview.contentItem.children[0])

                                        // childRectFile.z=0
                                        childRectText.i=0
                                        childRectText.ky=createdText.sky<kchildRect_listmodel.count?createdText.sky:kchildRect_listmodel.count-1
                                        childRectText.j=kchildRect_listmodel.get(0).childRect.count-1
                                        childRectText.xbegin=createdText.xbegin
                                        childRectText.xend=createdText.xend
                                        childRectText.baseHeightInt=createdText.height
                                        // childRectFile.kheight=baseHeight*
                                        // childRectFile.fileNotes=createdFile.notes
                                        // childRectFile.fileLocation=createdFile.location
                                        childRectText.kwidth=createdText.width
                                        compVector.push(childRectText)




                                        break;

                                    }

                                    }
                                }
                                Item{

                                    id:item_content

                                    width: parent.width
                                    height: parent.height-blackBottomHeight
                                    Component.onCompleted: {
                                        // console.log(kchildRect_listmodel.count)
                                        // console.log(type+" "+name)
                                        // console.log(childRect.count,0)
                                        // console.log(kchildRect_listmodel)
                                        for(var i=0;i<childRect.count;i++){
                                            var childRectNum=childRect.get(i)
                                            switch(childRectNum.type){
                                            case "File":{
                                                // console.log("height",childRectNum.height)
                                                // console.log(sky)

                                                var childRectFile= childRect_file.createObject(parent)
                                                childRectFile.i=index
                                                childRectFile.ky=childRectNum.sky<kchildRect_listmodel.count?childRectNum.sky:kchildRect_listmodel.count-1
                                                childRectFile.j=i
                                                childRectFile.xbegin=childRectNum.xbegin
                                                childRectFile.xend=childRectNum.xend
                                                childRectFile.baseHeightInt=childRectNum.height
                                                // childRectFile.kheight=baseHeight*
                                                childRectFile.fileNotes=childRectNum.notes
                                                childRectFile.fileLocation=childRectNum.location
                                                childRectFile.kwidth=childRectNum.width
                                                // console.log(childRectNum.sky,
                                                //             childRectFile.xbegin,
                                                //             childRectFile.baseHeightInt,
                                                //             childRectFile.kwidth
                                                //             )
                                                // console.log(childRectFile.kheight)
                                                // console.log(123)
                                                // kchildRect_listmodel.get(index).childRect.get(i).xbegin=0.1
                                                // console.log(kchildRect_listmodel_parent[0].get(index).childRect.get(i).xbegin)
                                                compVector.push(childRectFile)
                                                break;
                                            }
                                            case "Text":{
                                                var childRectText=childRect_text.createObject(parent)
                                                childRectText.i=index
                                                childRectText.textAreaText=childRectNum.text
                                                childRectText.ky=childRectNum.sky<kchildRect_listmodel.count?childRectNum.sky:kchildRect_listmodel.count-1
                                                childRectText.j=i
                                                childRectText.xbegin=childRectNum.xbegin
                                                childRectText.xend=childRectNum.xend
                                                childRectText.baseHeightInt=childRectNum.height
                                                // childRectFile.kheight=baseHeight*
                                                // childRectFile.fileNotes=childRectNum.notes
                                                // childRectFile.fileLocation=childRectNum.location
                                                childRectText.kwidth=childRectNum.width
                                                compVector.push(childRectText)
                                                break;
                                            }
                                            case "Table":{
                                                var childRectTable=childRect_table.createObject(parent)
                                                // console.log(childRectNum["model"].get(0))
                                                // ListModel tmp={};
                                                // tmp.append(childRectNum["model"][0])


                                                // if (!childRectTable.tableListModel) {
                                                //     childRectTable.tableListModel = Qt.createQmlObject('import QtQuick 2.0; ListModel {}', childRectTable);
                                                // }

                                                // 清空之前的模型数据
                                                // childRectTable.tableListModel.clear();

                                                // 将 childRectNum["model"] 的内容添加到 tableListModelAlias 中
                                                for (var j = 0; j < childRectNum["model"].count; j++) {
                                                    var modelItem = childRectNum["model"].get(j);
                                                    // 这里假设 modelItem 是一个对象，您可以根据需要调整属性
                                                    childRectTable.tableListModelAlias.append(modelItem);
                                                }


                                                childRectTable.i=index
                                                // childRectTable.textAreaText=childRectNum.text
                                                // childRectTable.kCol=childRectNum.Col
                                                // childRectTable.kRow=childRectNum.Row
                                                childRectTable.ky=childRectNum.sky<kchildRect_listmodel.count?childRectNum.sky:kchildRect_listmodel.count-1
                                                childRectTable.j=i
                                                childRectTable.xbegin=childRectNum.xbegin
                                                childRectTable.xend=childRectNum.xend
                                                childRectTable.baseHeightInt=childRectNum.height
                                                // childRectFile.kheight=baseHeight*
                                                // childRectFile.fileNotes=childRectNum.notes
                                                // childRectFile.fileLocation=childRectNum.location
                                                childRectTable.kwidth=childRectNum.width
                                                // console.log(childRectNum.model.count)
                                                // childRectTable.tableListModel=childRectNum["model"]
                                                // var newListModel = Qt.createQmlObject('import QtQuick 2.0; ListModel {}', parent);

                                                // for (var key in childRectNum["model"]) {
                                                //     childRectTable.tableListModelAlias.append(childRectNum["model"]["Row"]);
                                                // }
                                                // console.log(childRectNum["model"]["Row"])
                                                // var tempModel = Qt.createQmlObject('import QtQuick 2.0; ListModel {}', parent);
                                                //     console.log(childRectNum.model["Row"])
                                                //     for (var key in childRectNum.model) {
                                                //         // 假设 childRectNum.model 是对象数组，例如 [{name: "A"}, {name: "B"}]
                                                //         tempModel.append(childRectNum.model["Row"]);
                                                //     }
                                                console.log(childRectNum["model"].get(0))

                                                // childRectTable.tableListModel = newListModel;

                                                compVector.push(childRectText)




                                                break;
                                            }

                                            }


                                        }


                                    }
                                }

                                Rectangle{
                                    width: parent.width
                                    height: blackBottomHeight
                                    anchors.top: item_content.bottom
                                    color:"grey"

                                }
                            }



                        }




                    }


                }
            }
        }
        Rectangle{
            anchors.bottom: parent.bottom
            width: parent.width
            height: 40
            color:color_MaterialBlue
            Row{
                anchors.verticalCenter: parent.verticalCenter
                spacing:6
                anchors.left: parent.left
                height: parent.height
                width: 300
                Button{
                    anchors.verticalCenter: parent.verticalCenter

                    height:parent.height
                    width:200
                    text:editMode?"展览模式":"编辑模式"
                    Material.accent: Material.Blue
                    Material.roundedScale: Material.SmallScale
                    highlighted: true
                    onClicked: editMode=!editMode
                }
                Rectangle{
                    visible: editMode
                    // enabled: editMode
                    height: parent.height-5
                    anchors.verticalCenter: parent.verticalCenter
                    width: height
                    // color:"red"
                    Image{
                        anchors.fill: parent
                        source: "qrc:/ICON/icon/addLine.png"
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                kchildRect_listmodel.append({"childRect":[]})
                                // listModelToObject(kCodeNestTreeView_ListModel)//保存
                            }

                        }
                    }

                }




                Row{
                    anchors.verticalCenter: parent.verticalCenter
                    visible: editMode
                    spacing:6
                    height: parent.height-5
                    width: contentItem.childrenRect.width
                    Rectangle{
                        enabled: editMode
                        height: parent.height
                        anchors.verticalCenter: parent.verticalCenter
                        width: height
                        // color:compInt===0?"blue":"red"

                        Image{
                            anchors.fill: parent
                            source:compInt===0?"qrc:/ICON/icon/clickedFile.png":"qrc:/ICON/icon/normalFile.png"

                        }
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                compInt=0
                                // kchildRect_listmodel.append({"childRect":[]})
                                // listModelToObject(kCodeNestTreeView_ListModel)//保存
                            }

                        }
                    }
                    Rectangle{
                        enabled: editMode
                        height: parent.height
                        anchors.verticalCenter: parent.verticalCenter
                        width: height
                        // color:compInt===1?"blue":"red"
                        Image{
                            anchors.fill: parent
                            source:  compInt===1?"qrc:/ICON/icon/clickedText.png":"qrc:/ICON/icon/Text.png"
                            MouseArea{
                                anchors.fill: parent
                                onClicked: {
                                    compInt=1
                                    // kchildRect_listmodel.append({"childRect":[]})
                                    // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                }

                            }
                        }

                    }
                    Rectangle{
                        enabled: editMode
                        height: parent.height
                        anchors.verticalCenter: parent.verticalCenter
                        width: height
                        color:compInt===2?"blue":"red"
                        Image {

                            anchors.fill: parent

                            // source: compInt===2?"qrc:/ICON/icon/clickedText.png":"qrc:/ICON/icon/Text.png"
                            MouseArea{
                                anchors.fill: parent
                                onClicked: {
                                    compInt=2
                                    // kchildRect_listmodel.append({"childRect":[]})
                                    // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                }

                            }

                        }

                    }
                    // Rectangle{
                    //     enabled: editMode
                    //     height: parent.height
                    //     anchors.verticalCenter: parent.verticalCenter
                    //     width: height
                    //     color:compInt===3?"blue":"red"
                    //     MouseArea{
                    //         anchors.fill: parent
                    //         onClicked: {
                    //             compInt=3
                    //             // kchildRect_listmodel.append({"childRect":[]})
                    //             // listModelToObject(kCodeNestTreeView_ListModel)//保存
                    //         }

                    //     }
                    // }
                    Rectangle{
                        enabled: editMode
                        height: parent.height
                        anchors.verticalCenter: parent.verticalCenter
                        width: height
                        // color:"red"
                        Image {
                            anchors.fill: parent
                            source: "qrc:/ICON/icon/CLOSE.png"

                            MouseArea{
                                anchors.fill: parent
                                onClicked: {
                                    compInt=-1
                                    // kchildRect_listmodel.append({"childRect":[]})
                                    // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                }

                            }
                        }

                    }
                }




            }
            Rectangle{
                width: 150
                height: parent.height-5
                anchors.right: parent.right
                anchors.margins: 10
                color:"transparent"
                anchors.verticalCenter: parent.verticalCenter
                Slider {
                    Material.accent: "#FFFFFF"
                    anchors.fill: parent
                    from: 0
                    value: 1
                    to: 2
                    onValueChanged: {
                        // console.log(value)
                        scaleSize=value
                    }
                }

            }

        }

    }







}
